package demo;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

public class ItemSample {

	private List<SelectItem> items;
	static int counter = 5;

	private long dummyValue = 100;

	public long getDummyValue() {
		return dummyValue;
	}

	public void setDummyValue(long dummyValue) {
		this.dummyValue = dummyValue;
	}

	public List getOptionItems() {
		if (items == null) {
			initOptions();
		}
		return items;
	}



	/**
	 * chaque fois on ajoute une option dans la boite
	 * 
	 * @param event
	 */
	public void menuChanged(ValueChangeEvent event) {
		items.add(new SelectItem("o" + counter, "Option " + counter));
		counter++;
		FacesContext.getCurrentInstance().renderResponse(); // pour
															// court-circuiter
															// le cycle
	}

	public int getCounter() {
		return counter;
	}

	public String confirm() {
		System.out.println("confirmed");
		return "ok";
	}

	private void initOptions() {
		items = new ArrayList<SelectItem>();
		items.add(new SelectItem("o1", "Option 1"));
		items.add(new SelectItem("o2", "Option 2"));
		items.add(new SelectItem("o3", "Option 3"));
		items.add(new SelectItem("o4", "Option 4"));
	}
}
